package pers.kw.config.oauth;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;
import pers.kw.config.properties.OAuthConfigProperties;

import javax.annotation.Resource;

/**
 * 自定义客户端信息配置
 */
@Service
public class TestMyClientDetailService implements ClientDetailsService {

    @Resource
    private OAuthConfigProperties oAuthConfigProperties;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        BaseClientDetails clientDetails = new BaseClientDetails();
        OAuthConfigProperties.ClientInfo clientInfo = oAuthConfigProperties.getClientInfo();
        clientDetails.setClientId(clientId);
        //这里默认密码和ID一致，仅用于模拟不同客户端
        clientDetails.setClientSecret(passwordEncoder.encode(clientId));
        clientDetails.setScope(clientInfo.getScopes());
        clientDetails.setAuthorizedGrantTypes(clientInfo.getAuthorizedGrantTypes());
        //设置token过期时间
        clientDetails.setAccessTokenValiditySeconds(Integer.valueOf(clientId));
        clientDetails.setRefreshTokenValiditySeconds(Integer.valueOf(clientId));
        return clientDetails;
    }
}